        <div class="box">
            <div class="title">
                <h5><?=$title?></h5>
            </div>
            
            <div id="chartdiv" style="width: 100%;height:400px;"></div>
        </div>
        

    <link rel="stylesheet" href="amcharts/style.css" type="text/css" />
    <script src="amcharts/javascript/amcharts.js" type="text/javascript"></script>
    <script src="amcharts/javascript/raphael.js" type="text/javascript"></script>        
         <script type="text/javascript">
         // declaring variables
         var chart;
         var dataProvider;
         // this method called after all page contents are loaded
         window.onload = function() {
             createChart();            
             loadCSV("<?=base_url();?>data.txt");                                    
         }
         // method which loads external data

        function loadCSV(file) {
             if (window.XMLHttpRequest) {
                 // IE7+, Firefox, Chrome, Opera, Safari
                 var request = new XMLHttpRequest();
             }
             else {
                 // code for IE6, IE5
                 var request = new ActiveXObject('Microsoft.XMLHTTP');
             }
             // load
             request.open('GET', file, false);
             request.send();
             parseCSV(request.responseText);
         }
          // method which parses csv data
         function parseCSV(data){ 

             //replace UNIX new lines
             data = data.replace (/\r\n/g, "\n");
             //replace MAC new lines
             data = data.replace (/\r/g, "\n");
             //split into rows
             var rows = data.split("\n");
             // create array which will hold our data:
             dataProvider = [];
              // loop through all rows

            for (var i = 0; i < rows.length; i++){
                 // this line helps to skip empty rows
                 if (rows[i]) {                    
                     // our columns are separated by comma
                     var column = rows[i].split("|");  
					 
                      // column is array now 
                     // first item is date
                     var header = column[0];
                     // second item is value of the second column
                     var rencana = column[1];
                     // third item is value of the fird column 
                     var realisasi = column[2];
					// fourth item is value of the fird column 
                     var sisa = column[3];
					 var rencana_bal = column[4];
                     // third item is value of the fird column 
                     var realisasi_bal = column[5];
					// fourth item is value of the fird column 
                     var sisa_bal = column[6];
                     // create object which contains all these items:
                     var dataObject = {header:header, rencana:rencana, realisasi:realisasi, sisa:sisa, rencana_bal:rencana_bal, realisasi_bal:realisasi_bal, sisa_bal:sisa_bal};
                     // add object to dataProvider array
                     dataProvider.push(dataObject);
                 }
             }
             // set data provider to the chart
             chart.dataProvider = dataProvider;
             // this will force chart to rebuild using new data            
             chart.validateData();
         }

         // method which creates chart
         function createChart(){
             // chart variable is declared in the top
            chart = new AmCharts.AmSerialChart();
			chart.categoryField = "header";
            chart.marginLeft = 250;
            chart.marginTop = 30;
            chart.plotAreaBorderAlpha = 0.2;
            chart.rotate = true;			
  			 // chart must have a graph
            var graph = new AmCharts.AmGraph();
			graph.labelText="[[description]]";
			graph.descriptionField="rencana_bal";
			graph.title = "Anggaran";
            graph.type = "column";
            graph.lineAlpha = 0;
            graph.fillAlphas = 1;
            graph.lineColor = "#C72C95";
            graph.valueField = "rencana";			 
 			graph.balloonText="[[value]] %";
            chart.addGraph(graph);        
				
 			var graph = new AmCharts.AmGraph();
            graph.labelText="[[description]]";
			graph.descriptionField="realisasi_bal";			
			graph.title = "Realisasi";
            graph.type = "column";
            graph.lineAlpha = 0;
            graph.fillAlphas = 1;
            graph.lineColor = "#69A55C";
            graph.valueField = "realisasi";
			graph.balloonText="[[value]] %";
            chart.addGraph(graph);       
			
			var graph = new AmCharts.AmGraph();
            graph.labelText="[[description]]";
			graph.descriptionField="sisa_bal";			
			graph.title = "Deviasi";
            graph.type = "column";
            graph.lineAlpha = 0;
            graph.fillAlphas = 1;
            graph.lineColor = "#B5B8D3";
            graph.valueField = "sisa";
			graph.balloonText="[[value]] %";
            chart.addGraph(graph);  
             // 'chartdiv' is id of a container 
             // where our chart will be   
			 
			var valAxis = new AmCharts.ValueAxis();
            valAxis.stackType = "regular";
            valAxis.gridAlpha = 0.1;
            valAxis.axisAlpha = 0;
            chart.addValueAxis(valAxis);
                  
            var catAxis = chart.categoryAxis;
            catAxis.gridAlpha = 0.1;
            catAxis.axisAlpha = 0;
            catAxis.gridPosition = "start";
                  
            var legend = new AmCharts.AmLegend();
            legend.position = "right";
            legend.borderAlpha = 0.2;
            legend.horizontalGap = 10;
            legend.switchType = "v";
            chart.addLegend(legend);
				                     
            chart.write('chartdiv');
        }

    </script>
    
    
    <?php 
$terang = true;
$jenis=1; //rekap
$type=2; //month to date
$beban=2; //by dept
$coa="-";//coa==all
$department="-";
  
$bln=date("m");
$thn=date("Y");
$bulan=$bln;
  if($beban=="2"){
	$sql_dept="select distinct DeptID from budgetset where Tahun='$thn' and Bulan='$bln' order by DeptID";
	$query_dept=mysql_query($sql_dept);
	$x=0; 
	while($row_dept=mysql_fetch_array($query_dept)){
		$anggaranh=0; $realh=0; $anggaranh_thn=0; $realh_thn=0; 
		$anggaran1=0; $real1=0; $anggaran_thn=0; $real_thn=0; 
		$anggaran0=0; $real0=0;  $anggaran_thn0=0; $real_thn0=0; 
		
 		$sql_budget="select * from budget where Kelompok='BUDGET' order by NoUrut";
 		$query_budget=mysql_query($sql_budget);
		while($row_budget=mysql_fetch_array($query_budget)){
			if($row_budget['Jenis']=="D"){
				if($type=="1"){ //current month
					$sql_bs="select * from budgetset where Tahun='$thn' and Bulan='$bln' and MataAnggaran='$row_budget[MataAnggaran]' and DeptID='$row_dept[DeptID]'";
				}else{ //month to date
					$sql_bs="select sum(Anggaran) as t_anggaran, sum(Realisasi) as t_realisasi from budgetset where Tahun='$thn' and Bulan<='$bulan' and MataAnggaran='$row_budget[MataAnggaran]' and DeptID='$row_dept[DeptID]'";
				}
 				$query_bs=mysql_query($sql_bs);			
				while($row_bs=mysql_fetch_array($query_bs)){
					if($type=="1"){						 
						if(($coa!="" && $coa!="-" && $temp_index==$coa) || ($coa=="-")){
							$anggaran1=$anggaran1+$row_bs['Anggaran'];
							$real1=$real1+$row_bs['Realisasi'];		
							$anggaran0=$row_bs['Anggaran'];
							$real0=$row_bs['Realisasi'];			
 						} 					
					}else{
						if(($coa!="" && $coa!="-" && $temp_index==$coa) || ($coa=="-")){
							$anggaran1=$anggaran1+$row_bs['t_anggaran'];
							$real1=$real1+$row_bs['t_realisasi'];				
							$anggaran0=$row_bs['t_anggaran'];
							$real0=$row_bs['t_realisasi'];
						}//end if coa						
					}	
				}//end while row_bs
				
				$sql_tahun="select sum(Anggaran) as total_anggaran, sum(Realisasi) as total_realisasi from budgetset where Tahun='$thn' and MataAnggaran='$row_budget[MataAnggaran]' and DeptID='$row_dept[DeptID]'";
				$query_tahun=mysql_query($sql_tahun);
				while($row_tahun=mysql_fetch_array($query_tahun)){
					if(($coa!="" && $coa!="-" && $temp_index==$coa) || ($coa=="-")){
						$anggaran_thn=$anggaran_thn+$row_tahun['total_anggaran'];
						$real_thn=$real_thn+$row_tahun['total_realisasi'];
						
						$anggaran_thn0=$row_tahun['total_anggaran'];
						$real_thn0=$row_tahun['total_realisasi'];
					}//end if coa
					
				}
				
				if($anggaran_thn0!=0 || $real_thn0!=0){ 
 					
					if(($coa!="" && $coa!="-" && $temp_index==$coa) || ($coa=="-")){
						$anggaranh=$anggaranh+$anggaran0;
						$realh=$realh+$real0;
						$anggaranh_thn=$anggaranh_thn+$anggaran_thn0;
						$realh_thn=$realh_thn+$real_thn0;
					}//end if coa
					
 				}				
			}//end if Jenis='D'
			else if($row_budget['Jenis']=="H1"){				
				$temp_index=$row_budget['NoUrut'];
			}
			else if($row_budget['Jenis']=="H2"){
 				$temp_index=$row_budget['NoUrut'];
 			}
			else if($row_budget['Jenis']=="H3"){
 				$temp_index=$row_budget['NoUrut'];
 			}
			else if($row_budget['Jenis']=="H4"){
 				$temp_index=$row_budget['NoUrut'];
 			}
			else if($row_budget['Jenis']=="T1"){
 				$temp_index="";
 			}//end if jenis=T1
			else if($row_budget['Jenis']=="T2"){
 				$temp_index="";
 			}//end if jenis=T2
			else if($row_budget['Jenis']=="T3"){
 				$temp_index="";
 			}//end if jenis=T3
			else if($row_budget['Jenis']=="T4"){
 				$temp_index="";
 			}//end if jenis=T4
			else if($row_budget['Jenis']=="TH"){
  				$temp_index="";			 
			} //end else TH
 
		}//end while row_budget
		if($anggaranh!=0 || $realh!=0){
			$dept_header[$x]=$row_dept['DeptID'];
			$dept_anggaran[$x]=$anggaranh;
			$dept_real[$x]=$realh;
			$dept_anggaran_thn[$x]=$anggaranh_thn;
			$dept_real_thn[$x]=$realh_thn;
						
			$x++; 
		}
		
	}//end row while dept
	
	//begin sorting data
	$max_x=$x-1;
	for($w=0;$w<=$x;$w++){
		for($y=$max_x; $y>$w; $y--){
			if($dept_anggaran[$y] > $dept_anggaran[$y-1]){
				$tampung_header=$dept_header[$y];
				$tampung_anggaran=$dept_anggaran[$y];
				$tampung_real=$dept_real[$y];
				$tampung_anggaran_thn=$dept_anggaran_thn[$y];
				$tampung_real_thn=$dept_real_thn[$y];
				
				$dept_header[$y]=$dept_header[$y-1];
				$dept_header[$y-1]=$tampung_header;
				
 				$dept_anggaran[$y]=$dept_anggaran[$y-1];
				$dept_anggaran[$y-1]=$tampung_anggaran;
				
				$dept_real[$y]=$dept_real[$y-1];
				$dept_real[$y-1]=$tampung_real;
				
				$dept_anggaran_thn[$y]=$dept_anggaran_thn[$y-1];
				$dept_anggaran_thn[$y-1]=$tampung_anggaran_thn;
				
				$dept_real_thn[$y]=$dept_real_thn[$y-1];
				$dept_real_thn[$y-1]=$tampung_real_thn;
			}
		}
	}//end for sorting data	dept
	
    $data = "";
	for($l=0;$l<5;$l++){
		$ang_0=number_format($dept_anggaran[$l],2,',','.');
		$ang0=" ".$ang_0." ";
		$real_0=number_format($dept_real[$l],2,',','.');
		$real0=" ".$real_0." ";
		$sisa_00=$dept_anggaran[$l]-$dept_real[$l];
		$sisa_0=number_format($sisa_00,2,',','.');
		$sisa0=" ".$sisa_0." ";
		$total=$dept_anggaran[$l]+$dept_real[$l]+$sisa_00;
		if($total!=0){
			$ang=($dept_anggaran[$l]/$total)*100;
			$ang=round($ang,2);
			$real=($dept_real[$l]/$total)*100;
			$real=round($real,2);
			$sisa=($sisa_00/$total)*100;
			$sisa=round($sisa,2);
			if(($ang0!=0 && $ang0!="") || ($real0!=0 && $real0!="") || ($sisa0!=0 && $sisa0!="")){
                $data.= $dept_header[$l]."|".$ang."|".$real."|".$sisa."|".$ang0."|".$real0."|".$sisa0."\n";	
			}
		}
	}
		
	$FileName ="data.txt";
	$FileHandle = fopen($FileName, 'w') or die("can't open file");
				
	fwrite($FileHandle, $data);
	fclose($FileHandle);
}//end if beban dept
?> 